package com.itheima.mymap;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Dem03 {
    public static void main(String[] args) {
        //定义一个hashmap结合
        HashMap<String,String> hs = new HashMap<>();
        //向里面添加元素,put方法
        hs.put("张三","大儿子");
        hs.put("李四","二儿子");
        hs.put("王五","三儿子");
        hs.put("赵六","小儿子");
        System.out.println(hs);//{李四=二儿子, 张三=大儿子, 王五=三儿子, 赵六=小儿子}
        System.out.println("=====================");
        //遍历lambda表达式
        hs.forEach((key,value)->{
            System.out.println(key+"..."+value);
        });
        System.out.println("=====================");
        //键遍历
        Set<String> keys = hs.keySet();//获取所有键的集合
        for (String key : keys) {
            String value = hs.get(key);//根据简直来取值,键获取值
            //打印
            System.out.println(key+"..."+value);
        }
        System.out.println("=====================");
        //第三种遍历,利用获取键值对,然后用get(key),get(value)来进行打印
        Set<Map.Entry<String, String>> entries = hs.entrySet();//对象名点上entrySet()方法
        //对键值对进行获取遍历
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"="+value);
        }
          //获取所有的值的结合用对象点上Value
        Collection<String> values = hs.values();//所有对象
        Set<String> keys1 = hs.keySet();//获取所有键的集合
        //值遍历
        System.out.println("值遍历:");
        for (String value : values) {
            System.out.println(value);
        }
        //key遍历
        System.out.println("键遍历:");
        for (String s : keys1) {
            System.out.println(s);
        }
    }
}
